package com.mju.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.mju.dao.PaymentInfoDao;
import com.mju.pojo.PaymentInfo;
import com.mju.utils.GetTheCurrentTimeUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class PaymentInfoServiceImpl implements IPaymentInfoService {

    @Autowired
    private PaymentInfoDao paymentInfoDao;

    @SneakyThrows
    @Override
    public void createPaymentInfo(Map<String, String> params) {
        log.info("记录电脑端支付信息...");
        //获取订单号
        String orderNo = params.get("out_trade_no");
        // 支付宝交易号
        String transactionId =params.get("trade_no");
        // 交易状态
        String tradeStatus =params.get("trade_status");
        // 交易金额
        double totalAmount =new BigDecimal(params.get("total_amount")).doubleValue();
        // 付款时间
        String gmtPayment = params.get("gmt_payment");
        // 通过工具类转换成Date型数据
        Date createTime = GetTheCurrentTimeUtils.getTheEndTime(gmtPayment, 0);

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setPaymentType("支付宝");
        paymentInfo.setTradeType("电脑网站支付");
        paymentInfo.setTradeState(tradeStatus);
        paymentInfo.setPayerTotal(totalAmount);
        paymentInfo.setCreateTime(createTime);

        Gson gson = new Gson();
        String json = gson.toJson(params, HashMap.class);
        paymentInfo.setContent(json);

        paymentInfoDao.insert(paymentInfo);

    }

    @SneakyThrows
    @Override
    public void createPaymentInfoForMobileTerminal(Map<String, String> params) {
        log.info("记录移动端支付信息...");
        //获取订单号
        String orderNo = params.get("out_trade_no");
        // 支付宝交易号
        String transactionId =params.get("trade_no");
        // 交易状态
        String tradeStatus =params.get("trade_status");
        // 交易金额
        double totalAmount =new BigDecimal(params.get("total_amount")).doubleValue();
        // 付款时间
        String gmtPayment = params.get("gmt_payment");
        // 通过工具类转换成Date型数据
        Date createTime = GetTheCurrentTimeUtils.getTheEndTime(gmtPayment, 0);

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setPaymentType("支付宝");
        paymentInfo.setTradeType("移动端支付");
        paymentInfo.setTradeState(tradeStatus);
        paymentInfo.setPayerTotal(totalAmount);
        paymentInfo.setCreateTime(createTime);

        Gson gson = new Gson();
        String json = gson.toJson(params, HashMap.class);
        paymentInfo.setContent(json);

        paymentInfoDao.insert(paymentInfo);

    }

    @Override
    public IPage<PaymentInfo> queryAllPaymentInfo(Integer currentPage, Integer pageSize) {
        IPage page = new Page<>(currentPage, pageSize);
        paymentInfoDao.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<PaymentInfo> conQueryAllPaymentInfo(Integer currentPage, Integer pageSize, PaymentInfo paymentInfo) {
        IPage page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<PaymentInfo> lwq=new LambdaQueryWrapper<>();
        lwq.eq(!Objects.isNull(paymentInfo.getOrderNo()),PaymentInfo::getOrderNo,paymentInfo.getOrderNo());
        lwq.eq(!Objects.isNull(paymentInfo.getTransactionId()),PaymentInfo::getTransactionId,paymentInfo.getTransactionId());
        lwq.eq(!Objects.isNull(paymentInfo.getPayerTotal()),PaymentInfo::getPayerTotal,paymentInfo.getPayerTotal());
        lwq.eq(!Objects.isNull(paymentInfo.getTradeState()),PaymentInfo::getTradeState,paymentInfo.getTradeState());
        paymentInfoDao.selectPage(page,lwq);
        return page;
    }
}
